package com.fancy.projecty.C_历史流程实例;


import org.activiti.engine.*;
import org.activiti.engine.history.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * 查询历史流程实例
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class A_查询未完成的历史流程实例 {


	@Test
	public void c_获取当前用户未完结的流程实例() {
		/** 获取未完结的流程实例 **/
		List<HistoricProcessInstance> qingjia01 = historyService.
				createHistoricProcessInstanceQuery().unfinished()
				.processDefinitionKey("qingjia01")  //流程定义的key
				.startedBy("44")  //启动这个流程的用户id
				.orderByProcessInstanceEndTime().desc().list();
		System.out.println("qingjia01 = " + qingjia01);
	}


	@Test
	public void c_查询已经完成的流程实例() {
		// 可以通过 businessKey 关联业务表进行业务相关查询
		List<HistoricProcessInstance> qingjia01 = this.historyService.createHistoricProcessInstanceQuery().processDefinitionKey("qingjia01")
				.orderByProcessInstanceEndTime().desc()
				.list();

		System.out.println("qingjia01 = " + qingjia01);

	}


	/**
	 * 查询历史流程变量实例
	 */
	@Test
	public void c_根据processInstanceId查询() {
		String processInstanceId = "172508";
		List<HistoricVariableInstance> list = processEngine.getHistoryService()//
				.createHistoricVariableInstanceQuery()//创建一个历史的流程变量查询对象
				.processInstanceId(processInstanceId)//
				.list();
		if (list != null && list.size() > 0) {
			for (HistoricVariableInstance hvi : list) {
				System.out.println(hvi.getId() + "   " +
						hvi.getProcessInstanceId() + "   " +
						hvi.getVariableName() + "   " +
						hvi.getVariableTypeName() + "    " +
						hvi.getValue());
				System.out.println("###############################################");
			}
		}
	}


	/**
	 * 查询历史活动实例
	 */
	@Test
	public void c_根据processInstanceId查询历史活动节点() {
		String processInstanceId = "172508";
		List<HistoricActivityInstance> list = processEngine.getHistoryService()//
				.createHistoricActivityInstanceQuery()//创建历史活动实例的查询
				.processInstanceId(processInstanceId)//
				.orderByHistoricActivityInstanceStartTime().asc()//
				.list();
		if (list != null && list.size() > 0) {
			for (HistoricActivityInstance hai : list) {
				System.out.println(hai.getId() + "   " + hai.getProcessInstanceId() + "   " + hai.getActivityType() + "  " + hai.getStartTime() + "   " + hai.getEndTime() + "   " + hai.getDurationInMillis());
				System.out.println("#####################");
			}
		}

	}


	/**
	 * 查询历史流程实例
	 */
	@Test
	public void C_根据processInstanceId查询历史流程实例() {
		String processInstanceId = "172508";
		HistoricProcessInstance hpi = processEngine.getHistoryService()//与历史数据（历史表）相关的Service
				.createHistoricProcessInstanceQuery()//创建历史流程实例查询
				.processInstanceId(processInstanceId)//使用流程实例ID查询
				.orderByProcessInstanceStartTime().asc()
				.singleResult();
		System.err.println("-----------------");
		System.out.println(hpi.getId() + "    " + hpi.getProcessDefinitionId() + "    " + hpi.getStartTime() + "    " + hpi.getEndTime() + "     " + hpi.getDurationInMillis());
	}

	@Autowired
	private ProcessEngine processEngine;


	/**
	 * 查询历史任务
	 */
	@Test
	public void findHistoryTask() {
		String taskAssignee = "44";
		List<HistoricTaskInstance> list = this.historyService//与历史数据（历史表）相关的Service
				.createHistoricTaskInstanceQuery()//创建历史任务实例查询
				.taskAssignee(taskAssignee)//指定历史任务的办理人
				.list();

		if (list != null && list.size() > 0) {
			System.err.println("当前正在执行的流程实例");
			for (HistoricTaskInstance pi : list) {
				System.out.println("流程实例ID:" + pi.getId());//流程实例ID
				System.out.println("流程定义ID:" + pi.getProcessDefinitionId());//流程定义ID
				System.out.println("流程名称:" + pi.getName());
				System.out.println("代理人:" + pi.getAssignee());
			}
		} else {
			System.err.println("没有正在执行的流程实例");
		}
	}
//	输出：
//	流程实例ID:1404
//	流程定义ID:askForLeave:8:1304
//	流程名称:请假申请
//	代理人:小黄
//	流程实例ID:304
//	流程定义ID:askForLeave:2:204
//	流程名称:请假申请
//	代理人:小黄


	/**
	 * 查询流程状态（判断流程正在执行，还是结束）
	 */
	@Test
	public void c_查询流程实例是否走完() {
		String processInstanceId = "170001";
		//去正在执行的任务表查询
		ProcessInstance processInstance = this.runtimeService//表示正在执行的流程实例和执行对象
				.createProcessInstanceQuery()//创建流程实例查询
				.processInstanceId(processInstanceId)//使用流程实例ID查询
				.singleResult();

		if (processInstance == null) {
			System.out.println("该流程实例走完");
		} else {
			System.out.println("该流程实例还没走完");
		}
	}


	@Autowired
	private HistoryService historyService;
	@Autowired
	/*执行管理、包括启动、推进、删除流程实例等操作*/
	private RuntimeService runtimeService;


	@Autowired
	/*任务管理,可以做任务的查询的*/
	private TaskService taskService;

	@Autowired
	/*管理流程定义,部署相关的Service*/
	private RepositoryService repositoryService;
}
